//
//  PageScrollView.m
//  ClassManager
//
//  Created by ihope99 on 2017/8/11.
//  Copyright © 2017年 GZX. All rights reserved.
//

#import "PageScrollView.h"
#import "TYCyclePagerViewCell.h"

@implementation PageScrollView{
    NSArray *imageDataArray;
    CGFloat mWidth;
    CGFloat mHeight;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        mWidth = CGRectGetWidth(self.frame);
        mHeight = CGRectGetHeight(self.frame);
        
        mPagerView = [[TYCyclePagerView alloc]initWithFrame:self.bounds];
        mPagerView.isInfiniteLoop = YES;
        mPagerView.autoScrollInterval = 3.0;
        mPagerView.dataSource = self;
        mPagerView.delegate = self;
        // registerClass or registerNib
        [mPagerView registerClass:[TYCyclePagerViewCell class] forCellWithReuseIdentifier:@"cellId"];
        [self addSubview:mPagerView];
        
        mPageCtrl = [[TYPageControl alloc]initWithFrame:CGRectMake(0, mHeight - 30, mWidth, 30)];
        mPageCtrl.numberOfPages = 0;
        mPageCtrl.currentPageIndicatorSize = CGSizeMake(8, 8);
        [self addSubview:mPageCtrl];
        
        
        
    }
    return self;
}

-(void)setMImagePathArray:(NSArray *)mImagePathArray{
    _mImagePathArray = mImagePathArray;
    imageDataArray = [NSArray arrayWithArray:mImagePathArray];
    mPageCtrl.numberOfPages = imageDataArray.count;
    [mPagerView reloadData];
}
#pragma mark - TYCyclePagerViewDataSource

- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    return imageDataArray.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    TYCyclePagerViewCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndex:index];
    [cell reloadData:imageDataArray[index]];
    return cell;
}

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
    layout.itemSize = CGSizeMake(CGRectGetWidth(pageView.frame), CGRectGetHeight(pageView.frame));
    layout.itemSpacing = 5;
    layout.itemHorizontalCenter = YES;
    return layout;
}

- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    mPageCtrl.currentPage = toIndex;
}

- (void)pageControlValueChangeAction:(TYPageControl *)sender {
    DLog(@"pageControlValueChangeAction: %ld",sender.currentPage);
}


//#pragma mark - action
//
//- (IBAction)switchValueChangeAction:(UISwitch *)sender {
//    if (sender.tag == 0) {
//        _pagerView.isInfiniteLoop = sender.isOn; 是否可以循环
//        [_pagerView updateData];
//    }else if (sender.tag == 1) {
//        _pagerView.autoScrollInterval = sender.isOn ? 3.0:0;
//    }else if (sender.tag == 2) {
//        _pagerView.layout.itemHorizontalCenter = sender.isOn;
//        [UIView animateWithDuration:0.3 animations:^{
//            [_pagerView setNeedUpdateLayout];
//        }];
//    }
//}
//
//- (IBAction)sliderValueChangeAction:(UISlider *)sender {
//    if (sender.tag == 0) {
//        _pagerView.layout.itemSize = CGSizeMake(CGRectGetWidth(_pagerView.frame)*sender.value, CGRectGetHeight(_pagerView.frame)*sender.value); 改变item大小
//        [_pagerView setNeedUpdateLayout];
//    }else if (sender.tag == 1) {
//        _pagerView.layout.itemSpacing = 30*sender.value; 改变item间距
//        [_pagerView setNeedUpdateLayout];
//    }else if (sender.tag == 2) {
//        _pageControl.pageIndicatorSize = CGSizeMake(6*(1+sender.value), 6*(1+sender.value));
//        _pageControl.currentPageIndicatorSize = CGSizeMake(8*(1+sender.value), 8*(1+sender.value));
//        _pageControl.pageIndicatorSpaing = (1+sender.value)*10;   改变pageControl大小
//    }
//}
//
//- (IBAction)buttonAction:(UIButton *)sender {
//    _pagerView.layout.layoutType = sender.tag; //修改显示类型
//    [_pagerView setNeedUpdateLayout];
//}
//
//- (void)pageControlValueChangeAction:(TYPageControl *)sender {
//    NSLog(@"pageControlValueChangeAction: %ld",sender.currentPage);
//}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
